package com.scf.user.controller;



import com.aliyun.oss.OSSClient;
import com.scf.uitl.ResponseResult;
import com.scf.user.entity.File;
import com.scf.user.entity.FileVo;
import com.scf.user.service.FileService;
import com.scf.user.util.UploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author author
 * @create 2021-11-16-16:08
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private OSSClient ossClient;    //OSS客户端
    @Resource
    private FileService fileService;

    private static final String BUCKETNAME="images-ajiboom";//桶名
    private static final String BASEPATH="https://images-ajiboom.oss-cn-beijing.aliyuncs.com/";//阿里云网络路径

    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file,@RequestParam FileVo plid){

        //1.获取到文件名字
        String fileName=file.getOriginalFilename();
        //得到后置名
        String suffix=fileName.substring(fileName.indexOf("."));
        //新名字
        fileName = UUID.randomUUID().toString()+suffix;


        //2.开始上传文件
        InputStream inputStream=null;
        try {
            inputStream = file.getInputStream();
            //设置上传得参数
            ossClient.putObject(BUCKETNAME,fileName,inputStream);

            //设置过期时间    付费得就不需要  30天
            Date date = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30);

            UploadResult result=new UploadResult();

            result.setFileName(fileName);
            result.setUrl(BASEPATH+fileName);

            //上传文件
            ossClient.generatePresignedUrl(BUCKETNAME,fileName,date);
            File file2 = new File();
            file2.setAname(fileName);
            file2.setAurl(BASEPATH+fileName);

            file2.setPlid(Integer.parseInt(plid.getPlid()));
            fileService.insert(file2);
            ResponseResult responseResult=new ResponseResult();
            responseResult.setCode(200);
            responseResult.setMessage("上传成功");
            responseResult.setData(result);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //删除文件
    @GetMapping("/del")
    public ResponseResult del(String fileName){
        ossClient.deleteObject(BUCKETNAME,fileName);
        return null;
    }

//    public String getUrl(String key) {
//        // 设置URL过期时间为10年  3600l* 1000*24*365*10
//        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
//        // 生成URL
//        URL url = ossClient.generatePresignedUrl(BUCKETNAME, key, expiration);
//        if (url != null) {
//            System.out.println(url);
//            return url.toString();
//        }
//        return null;
//    }

    @GetMapping("/file/{plid}")
    public ResponseResult getUrl(@PathVariable("plid") int plid){
        List<File> select = fileService.select(plid);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(select);
        responseResult.setCode(200);
        responseResult.setMessage("图片查询成功");
        return responseResult;
    }
}
